﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model.Course;
using Model.User;

public partial class ViewTask : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["TaskId"] == null) return;
        Student student = Student.get(Convert.ToInt32(Session["Person"]));

        Task t = Task.get(Convert.ToInt32(Session["TaskId"]));

        if (t != null)
        {
            lectureNameLabel.Text = t.getName();
            lectureNameLabel.Font.Size = FontUnit.Parse("1.5em");
            startLabel.Text = "Start zadania: " + t.getStartTime().ToShortDateString();
            endLabel.Text = "Koniec zadania: " + t.getEndTime().ToShortDateString();
            content.InnerHtml = t.getContent();
        }

        if (Teacher.get(Convert.ToInt32(Session["Person"])) != null)
        {
            toolbarButtonDelete.Visible = true;
            studentListLabel.Visible = true;

            Course c = Course.get(Convert.ToInt32(Session["CourseId"]));
            Student[] students = c.getSignedStudents();

            if (studentList.Items.Count <= 0)
            {
                foreach (var s in students)
                {
                    ListItem li = new ListItem(s.getFirstName() + " " + s.getLastName(), s.getId().ToString());
                    studentList.Items.Add(li);
                }
            }
            studentListButton.Click += studentListButton_Click;

            SolvedTask st = SolvedTask.get(students[0], Task.get(Convert.ToInt32(Session["TaskId"])));
            if (st != null)
            {
                Mark.Text = "Ocena: " + st.getMark();
                solvedContent.InnerHtml = st.getContent();
            }
        }
        else if (student != null)
        {
            if (DateTime.Now.CompareTo(t.getStartTime()) > 0 && DateTime.Now.CompareTo(t.getEndTime()) < 0)
                toolbarButtonAddAnswer.Visible = true;

            SolvedTask st = SolvedTask.get(student, Task.get(Convert.ToInt32(Session["TaskId"])));
            if (st != null)
            {
                Mark.Text += st.getMark();
                solvedContent.InnerHtml = st.getContent();
            }
        }


    }

    void studentListButton_Click(object sender, EventArgs e)
    {
        SolvedTask st = SolvedTask.get(Student.get(Convert.ToInt32(studentList.SelectedValue)),
                                       Task.get(Convert.ToInt32(Session["TaskId"])));
        if (st != null)
        {
            Mark.Text = "Ocena: " + st.getMark();
            solvedContent.InnerHtml = st.getContent();
        }
        else
        {
            Mark.Text = "Ocena: ";
            solvedContent.InnerHtml = "";
        }
    }

    protected void toolbarButtonDelete_Command(object sender, CommandEventArgs e)
    {
        Task l = Task.get(Convert.ToInt32(Session["TaskId"]));
        l.delete();

        Session["ActiveMenuButton"] = "menuButton7";
        Response.Redirect("~/CourseView/ViewCourseTasks.aspx");
    }
    protected void toolbarButtonAddAnswer_Click(object sender, ImageClickEventArgs e)
    {
        Session["ActiveMenuButton"] = "menuButton6";
        Response.Redirect("~/CourseView/CreateSolvedTaskView.aspx");
    }
}